package handler

import (
	"math/rand"
	"svvenus/conf"
	"svvenus/util/uttype"
	"svvenus/vgame/logic"
	"svvenus/vpub/constants"
	"svvenus/vpub/serve"
	"svvenus/znet"
	"svvenus/znet/zpb"
	"time"
)

// CGPlayerLogin
// @Description: 玩家登入
// @param request
func CGPlayerLogin(request *znet.Request) {
	rsp := &zpb.MsgNil{}
	rid := zpb.EPid_GCPlayerLogin
	var req zpb.C2GPlayerLogin
	if !request.GetPb(&req) {
		return
	}

	if constants.ServerLocked {
		request.Send(rid, conf.EProtocolResult登陆受限, rsp)
		return
	}
	if req.UserId == "" {
		request.Send(rid, conf.EProtocolResult账号长度不符, rsp)
		return
	}
	if logic.ConnHasPlayer(request.GetConnection()) {
		request.Send(rid, conf.EProtocolResult玩家已登录勿重复, rsp)
		return
	}

	pid, ok := logic.PlayerGetPidFormUserId(req.UserId)
	if !ok {
		request.Send(rid, conf.EProtocolResult玩家不存在, rsp)
		return
	}
	player := logic.PlayerGet(pid, logic.PlayerDataGetModeAll)
	if player == nil {
		request.Send(rid, conf.EProtocolResult玩家不存在, rsp)
		return
	}

	//如果玩家存在 但是和原来的连接不符 通知原玩家被替换
	if request.GetConnection().ConnID != player.ConnID {
		//通知原玩家被替换
		{
			player.SendId(zpb.EPid_GCPlayerReplace)
		}
		//解除绑定
		player.UnsetConn()
	}

	logic.PlayerLogin(player)

	//玩家绑定连接
	player.SetConn(request.GetConnection())

	request.Send(rid, 0, rsp)
}

// CGPlayerLogout
// @Description: 玩家登出
// @param request
func CGPlayerLogout(request *znet.Request) {
	rid := zpb.EPid_GCPlayerLogout

	player := logic.ConnGetPlayer(request.GetConnection())
	if player == nil {
		request.SendId(rid)
		return
	}

	logic.PlayerDisconnection(player)
	logic.MapPlayer.Delete(player.IdString())

	request.SendId(rid)
}

// CGPlayerCreate
// @Description: 玩家创建
// @param request
func CGPlayerCreate(request *znet.Request) {
	rsp := &zpb.MsgNil{}
	rid := zpb.EPid_GCPlayerCreate
	var req zpb.C2GPlayerCreate
	if !request.GetPb(&req) {
		return
	}

	if req.UserId == "" {
		request.Send(rid, conf.EProtocolResult账号长度不符, rsp)
		return
	}
	if logic.ConnHasPlayer(request.GetConnection()) {
		request.Send(rid, conf.EProtocolResult玩家已登录勿重复, rsp)
		return
	}

	db := constants.MgoMain.New()
	defer db.Close()

	_, ok := logic.PlayerGetPidFormUserId(req.UserId)
	if ok {
		request.Send(rid, conf.EProtocolResult玩家已经被注册, rsp)
		return
	}
	if req.Name == "8i9o0p" {
		req.Gender = rand.Int31n(2)
		req.Name = logic.GetRandomName(req.Gender)
	}

	checkName := logic.PlayerCheckName(req.Name)
	if checkName != 0 {
		request.Send(rid, checkName, rsp)
		return
	}

	player := &logic.Player{}
	logic.PlayerCreate(player, &req)

	logic.PlayerLogin(player)

	//添加测试邮件
	for i := 0; i < 3; i++ {
		mail := logic.NewMail()
		mail.Title = "测试"
		mail.Body = "哈哈" + uttype.IntToString(i)
		mail.CreateTime = time.Now()
		mail.AddAward(conf.PcAward{Type: conf.EAwardType货币, ConfigId: conf.EMoneyType金币, Count: 888})
		player.MailAddToPlayer(mail)
	}

	if !player.Save(db, true) {
		request.Send(rid, conf.EProtocolResult服务器数据库异常, rsp)
		return
	}
	//玩家绑定连接
	player.SetConn(request.GetConnection())

	//记录
	serve.RecordAdd(zpb.RecType_rtPlayerCreate, zpb.RecSource_rsPlayerCreate, player.IdString(), player.Name)

	request.Send(rid, 0, rsp)
}
